package com.atguigu.admin.service.impl;

import com.atguigu.admin.service.UserService;
import com.atguigu.clients.UserClient;
import com.atguigu.param.PageParam;
import com.atguigu.pojo.User;
import com.atguigu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    // 微服务调用
    @Autowired
    private UserClient userClient;

    @Cacheable(value = "list.user",key = "#pageParam.currentPage+'-'+#pageParam.getPageSize()")
    @Override
    public R list(PageParam pageParam) {

        return userClient.list(pageParam);
    }

    //删除,清空缓存集合
    //删除,清空对应单条id的数据
    @Caching(
            evict = {
                    @CacheEvict(value = "list.user",allEntries = true),
                    @CacheEvict(value = "user",key = "#userId")
            }
    )
    @Override
    public R remove(Integer userId) {

        return userClient.remove(userId);
    }

    @Caching(
            evict = {
                    @CacheEvict(value = "list.user",allEntries = true),
                    @CacheEvict(value = "user",key = "#user.userId")
            }
    )
    @Override
    public R update(User user) {

        return userClient.update(user);
    }

    @Caching(
            evict = {
                    @CacheEvict(value = "list.user",allEntries = true)
            }
    )
    @Override
    public R save(User user) {

        return userClient.save(user);
    }
}
